﻿using Aspose.Words;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace 报表导出.Common
{
    public class CommonWords
    {
        public static string Word(string TemplateName, string SaveName, List<WordsTemp> dic)
        {
            //实例化模板实例
            Document doc = new Document(TemplateName);
            foreach (var key in dic)
            {
                string repStr = string.Format("&{0}&", key.Name);

                //图片替换
                if (key.Type == FileFormat.Image)
                {
                    //if (key.Value == "暂无照片")
                    //{
                    //    Regex reg = new Regex(repStr);
                    //    var img = new ReplaceAndInsertImage(System.Web.HttpContext.Current.Server.MapPath("\\Word\\Image\\暂无图片.jpg"));
                    //    doc.Range.Replace(reg, img, false);
                    //}
                    //else if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(key.Value)) == false)
                    //{
                    //    doc.Range.Replace(repStr, "图片地址错误", false, false);
                    //}
                    //else
                    //{
                    //    Regex reg = new Regex(repStr);
                    //    var img = new ReplaceAndInsertImage(System.Web.HttpContext.Current.Server.MapPath(key.Value));
                    //    doc.Range.Replace(reg, img, false);
                    //}
                }
                else
                {
                    //文字替换
                    doc.Range.Replace(repStr, key.Value, false, false);
                }
            }
            doc.Save(SaveName);//也可以保存为1.doc 兼容03-07
            return SaveName;
        }
    }
}